﻿using System;
using WorkOrder.Model;
using WorkOrder.Business;
using System.Drawing;

namespace WorkOrder.Notice
{
    public partial class addNoticeType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Model_Table_Notice_Type model;
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(GetType(), "windohref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.parent.location.href='../login.aspx';</script>");
            }
            if (Page.IsPostBack)
            {
                //参数a的值的意义：0保存
                if (Request.Params["a"] == "0")
                {
                    if (!string.IsNullOrEmpty(hidID.Value))//更新已修改的记录
                    {
                        if (!new Business_Table_Permission().HasModify(Session["UserID"].ToString(), "Table_Notice_type"))
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：您没有修改公告类别信息的权限！";
                            ClientScript.RegisterClientScriptBlock(GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有修改公告类别信息的权限！');window.close();</script>");
                            return;
                        }
                        model = new Model_Table_Notice_Type(hidID.Value, Server.HtmlEncode(txtName.Text.Trim()), Server.HtmlEncode(txtRemark.Text.Trim()));
                        if (new Business_Table_Notice_type().Update(model) > 0)
                        {
                            hidName.Value = hidName != null ? txtName.Text.Trim() : "";
                            hidRemark.Value = hidRemark != null ? txtRemark.Text.Trim() : "";
                            hidID.Value = "";
                            txtName.Text = "";
                            txtRemark.Text = "";
                            txtName.Enabled = false;
                            txtRemark.Enabled = false;
                            lblMsg.ForeColor = Color.DarkGreen;
                            lblMsg.Text = "提示信息：成功保存一条记录！";
                        }
                    }
                    else
                    {
                        if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Debugging_unit"))
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：您没有新增公告类型的权限！";
                            ClientScript.RegisterClientScriptBlock(GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有新增公告类型的权限！');window.close();</script>");
                            return;
                        }

                        model = new Model_Table_Notice_Type("0", Server.HtmlEncode(txtName.Text.Trim()), Server.HtmlEncode(txtRemark.Text.Trim()));
                        if (new Business_Table_Notice_type().Add(model) > 0)
                        {
                            hidName.Value = "";
                            hidRemark.Value = "";
                            hidID.Value = "";
                            txtName.Text = "";
                            txtRemark.Text = "";
                            lblMsg.ForeColor = Color.DarkGreen;
                            lblMsg.Text = "提示信息：成功保存一条记录！";
                        }
                    }
                }
            }
            //加载编辑的资料
            if (Request.Params["a"] == "1")
            {
                if (!String.IsNullOrEmpty(Request.Params["b"]))
                {
                    try
                    {
                        model = new Business_Table_Notice_type().GetModel(Convert.ToInt32(Request.Params["b"]));
                        if (model != null)
                        {
                            txtName.Text = model.TypeName;
                            txtRemark.Text = model.Remark;
                            hidID.Value = model.TypeID;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    
                }
            }
        }
    }
}